/*
 * 
 */
package com.mvc.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.EntityDao;
import com.mvc.entity.Loan;
import com.pub.Page;
import com.pub.StrUtils;

/**
 * 仓单质押贷款服务
 * 
 * @author Hervey Hall
 *
 */
@Service
public class LoanService {
	@Autowired
	EntityDao dao;

	/**
	 * 增加一条新的贷款信息
	 * 
	 * @param loan
	 *            贷款信息
	 */
	public void save(Loan loan) {
		dao.save(loan);
	}

	/**
	 * 获取贷款信息列表
	 * 
	 * @param page
	 *            页数
	 * @return 贷款信息列表
	 */
	public List<Object> getLoanList(Page page) {
		return dao.pageQuery("select a from Loan a order by a.ID desc", page.getPageNo(), page.getPageSize());
	}

	/**
	 * 根据ID获取贷款信息
	 * 
	 * @param id
	 *            贷款信息ID
	 * @return 贷款信息
	 */
	public Loan getLoan(Long id) {
		return (Loan) dao.loadById(Loan.class, id);
	}

	/**
	 * 根据ID删除一条贷款信息
	 * 
	 * @param id
	 *            贷款信息ID
	 */
	public void deleteLoan(Long id) {
		dao.delById(Loan.class, id);
	}

	/**
	 * 根据条件获取贷款信息的数量
	 * 
	 * @param condition
	 *            条件
	 * @param page
	 *            分页对象
	 * @return 数量
	 */
	public long getLoanCount(String condition, Page page) {
		int count = 0;
		if (!StrUtils.isNull(condition)) {
			condition = new StringBuilder(" where ").append(condition).toString();
		}
		count = dao.countQuery(new StringBuilder("select count(*) from Loan a").append(condition).toString());
		if ((count % page.getPageSize()) == 0) {
			count = count / page.getPageSize();
		} else {
			count = count / page.getPageSize() + 1;
		}
		return count;
	}
}
